#!/bin/sh
#	$OpenBSD: postinst,v 1.6 2006/04/04 00:25:58 uwe Exp $
#
# Copyright (c) 2005 Uwe Stuehler <uwe@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

cd /root/etc/rc.d || exit $?

rmmod zbsdmod 2>/dev/null

mount -o remount,rw /

# The C3100/C3200 models have a 32M root filesystem in NAND flash,
# but it is completely full. Move things around to make some room.
rootsz=`df -k / | awk 'NR==2{print $2}'`
if [ ${rootsz:-0} -ge 32768 ]; then
	if [ -f /usr/QtPalmtop.rom/etc/kanwadict ]; then
		# C3100
		rm -f /home/QtPalmtop/etc/kanwadict
		mv /usr/QtPalmtop.rom/etc/kanwadict /home/QtPalmtop/etc/kanwadict
	else
		# C3200
		if [ -L /home/QtPalmtop/etc/speech -a \
		     -d /usr/QtPalmtop.rom/etc/speech ]; then
			rm -f /home/QtPalmtop/etc/speech
			mv /usr/QtPalmtop.rom/etc/speech /home/QtPalmtop/etc
			ln -s /home/QtPalmtop/etc/speech /usr/QtPalmtop.rom/etc
		fi
		# Copying bsd.rd onto a disk partition significantly
		# increases the chance that zbsdmod.o can boot it.
		hddsz=`df -k /hdd2 | awk 'NR==2{print $4}'`
		if [ ${hddsz:-0} -ge 5120 ]; then
			cp /usr/local/OpenBSD/bsd.rd /hdd2
		fi
	fi
fi

if [ ! -f rc.rofilesys.openbsd ]; then
	cp -p rc.rofilesys rc.rofilesys.openbsd
fi

cat rc.rofilesys | sed -e '/vfat/s/^/#/' > tmp || exit $?
mv tmp rc.rofilesys
cat rc.rofilesys | sed -e '/^LINUXFMT=ext3$/s/^/#/' \
    -e '/^#LINUXFMT=ext2$/s/^#//' > tmp || exit $?
mv tmp rc.rofilesys
cat rc.rofilesys | sed \
    -e '/\/pcmcia start$/s,$,; sh /root/etc/rc.d/rc.zboot,' > tmp || exit $?
mv tmp rc.rofilesys
chown root:root rc.rofilesys
chmod 775 rc.rofilesys

# Convert all mounted ext3 filesystems back into ext2 on reboot.
for dev in `mount | awk '/on \/hdd[123] type ext3/ {print $1}'`; do
	cat <<- EOF | debugfs
	open -f -w ${dev}
	features -has_journal -needs_recovery
	quit
EOF
done

rm -f rc.zboot
rm -f zboot
rm -f zbsdmod.o
cp /usr/local/OpenBSD/rc.zboot .
cp /usr/local/OpenBSD/zboot .
cp /usr/local/OpenBSD/zbsdmod.o .

sync; sleep 3
mount -o remount,ro /

insmod /usr/local/OpenBSD/zbsdmod.o
